﻿using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using MTS;
using System.Diagnostics;

namespace SH
{
    public class shLog
    {

        const uint SH_DEBUG_LEVEL_NONE = 0; /// 不输出任何调试信息
        const uint SH_DEBUG_LEVEL_FATAL = 1; /// 严重错误
        const uint SH_DEBUG_LEVEL_ERROR = 2; /// 一般错误
        const uint SH_DEBUG_LEVEL_WARNING = 3; /// 警告消息
        const uint SH_DEBUG_LEVEL_SUCCESS = 4; /// 成功消息
        const uint SH_DEBUG_LEVEL_INFO = 5; /// 一般消息
        const uint SH_DEBUG_LEVEL_DEBUG = 6; /// 简单调试信息
        const uint SH_DEBUG_LEVEL_VERBOSE = 7; /// 详细调试信息
        const uint SH_DEBUG_LEVEL_ALWAYS = 8; /// 记录所有的信息

       public static void TraceFatal(string fmt, params object[] args)
        {
            var str = String.Format(fmt, args);

            StackTrace st = new StackTrace(true);
            StackFrame sf = st.GetFrame(1);

            mtCLogParam param = new mtCLogParam
            {
                level = SH_DEBUG_LEVEL_FATAL,
                file = sf.GetFileName(),
                func = sf.GetMethod().Name,
                line = (uint)sf.GetFileLineNumber(),
                szStr = Marshal.StringToHGlobalAnsi(str)
            };

        }

        public static void TraceError(string fmt, params object[] args)
        {
            var str = String.Format(fmt, args);

            StackTrace st = new StackTrace(true);
            StackFrame sf = st.GetFrame(1);

            mtCLogParam param = new mtCLogParam
            {
                level = SH_DEBUG_LEVEL_ERROR,
                file = sf.GetFileName(),
                func = sf.GetMethod().Name,
                line = (uint)sf.GetFileLineNumber(),
                szStr = Marshal.StringToHGlobalAnsi(str)
            };

        }

        public static void TraceWarning(string fmt, params object[] args)
        {
            var str = String.Format(fmt, args);

            StackTrace st = new StackTrace(true);
            StackFrame sf = st.GetFrame(1);

            mtCLogParam param = new mtCLogParam
            {
                level = SH_DEBUG_LEVEL_WARNING,
                file = sf.GetFileName(),
                func = sf.GetMethod().Name,
                line = (uint)sf.GetFileLineNumber(),
                szStr = Marshal.StringToHGlobalAnsi(str)

            };

            TraceOut(param);
        }

        public static void TraceSuccess(string fmt, params object[] args)
        {
            var str = String.Format(fmt, args);

            StackTrace st = new StackTrace(true);
            StackFrame sf = st.GetFrame(1);

            mtCLogParam param = new mtCLogParam
            {
                level = SH_DEBUG_LEVEL_SUCCESS,
                file = sf.GetFileName(),
                func = sf.GetMethod().Name,
                line = (uint)sf.GetFileLineNumber(),
                szStr = Marshal.StringToHGlobalAnsi(str)

            };

            TraceOut(param);
        }

        public static void TraceInfo(string fmt, params object[] args)
        {
            var str = String.Format(fmt, args);

            StackTrace st = new StackTrace(true);
            StackFrame sf = st.GetFrame(1);

            mtCLogParam param = new mtCLogParam
            {
                level = SH_DEBUG_LEVEL_INFO,
                file = sf.GetFileName(),
                func = sf.GetMethod().Name,
                line = (uint)sf.GetFileLineNumber(),
                szStr = Marshal.StringToHGlobalAnsi(str)

            };

            TraceOut(param);
        }

        public static void TraceDebug(string fmt, params object[] args)
        {
            var str = String.Format(fmt, args);

            StackTrace st = new StackTrace(true);
            StackFrame sf = st.GetFrame(1);

            mtCLogParam param = new mtCLogParam
            {
                level = SH_DEBUG_LEVEL_DEBUG,
                file = sf.GetFileName(),
                func = sf.GetMethod().Name,
                line = (uint)sf.GetFileLineNumber(),
                szStr = Marshal.StringToHGlobalAnsi(str)

            };

            TraceOut(param);
        }

        public static void TraceVerbose(string fmt, params object[] args)
        {
            var str = String.Format(fmt, args);

            StackTrace st = new StackTrace(true);
            StackFrame sf = st.GetFrame(1);

            mtCLogParam param = new mtCLogParam
            {
                level = SH_DEBUG_LEVEL_VERBOSE,
                file = sf.GetFileName(),
                func = sf.GetMethod().Name,
                line = (uint)sf.GetFileLineNumber(),
                szStr = Marshal.StringToHGlobalAnsi(str)

            };

            TraceOut(param);

        }

        public static void TraceOut(mtCLogParam param)
        {
            MTSDLLWrap.getInstance().mtTraceOut(ref param);

            Marshal.FreeHGlobal(param.szStr);
        }

    }
}
